package com.hundsun.cprs.yyt.gateway.aop;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSON;

/**
 * 简单接口模块日志拦截打印
 * @author linrl
 * @date 2018年4月20日
 */
@Aspect
@Component
public class LogAspect {
    
    private Log logger = LogFactory.getLog("gatewayAop");
    
    /**
     * 拦截所有gateway模块 的所有接口
     */
    @Pointcut("execution(* com.hundsun.cprs.yyt.gateway.service..*.*(..)) && !execution(* com.hundsun.cprs.yyt.gateway.service..*BankBindingService.*(..)) && !execution(* com.hundsun.cprs.yyt.gateway.service.trade.TradeCommonService.getTradeMatchDetailByGoods(..))")
    public void accessLogInGatewayModule(){};
    
    @Around(value = "accessLogInGatewayModule()")
    public Object accessLogInGatewayCalls(ProceedingJoinPoint joinPoint){
        Object result = null;
        
        try{
            String targetName = joinPoint.getTarget().getClass().getName();
            String methodName = joinPoint.getSignature().getName();
            Object[] args = joinPoint.getArgs();
            if(args != null && args.length > 0){
                logger.info(new StringBuilder(targetName).append(".").append(methodName).append("(req):").append(JSON.toJSONString(joinPoint.getArgs())).toString());
            }
            result = joinPoint.proceed();
            
            if(result != null){
                logger.info(new StringBuilder(targetName).append(".").append(methodName).append("(resp):").append(JSON.toJSONString(result)).toString());
            }
        } catch (Throwable e) {
            e.printStackTrace();
        }
        return result;
    }
}
